<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of welcome
 *
 * @author Obi
 */
class Admin_katagori extends MY_Controller {

    function __construct() {
        parent::__construct();
        
        $cek_admin = $this->session->userdata('cek_admin');
        if (!$cek_admin)
            redirect("admin");

        $this->mkatagori = new M_katagori();
    }

    function index() {

        $data['title'] = 'Data Kategori Produk';
		
        $data['list'] = $this->mkatagori->select_data();

        $data['isi'] = 'isi';
        $this->load->view('template_admin', $data);
    }

    function tambah() {

        $data['title'] = 'Tambah Katagori';

        $data['id'] = "";
        $data['kode_katagori'] = "";
        $data['nama_katagori'] = "";

        $data['isi'] = 'form';
        $this->load->view('template_admin', $data);
    }

    function edit($id = NULL) {

        $data['title'] = 'Ubah Katagori';

        $dt = $this->mkatagori->where("id = '$id'")->get();

        $data['id'] = $dt->id;
        $data['kode_katagori'] = $dt->kode_katagori;
        $data['nama_katagori'] = $dt->nama_katagori;
        $data['parent_katagori'] = $dt->parentid;
        $data['child_katagori'] = $this->mkatagori->check_child($id);

        $data['isi'] = 'form';
        $this->load->view('template_admin', $data);
    }

    function delete($id) {
        $this->mkatagori->delete($id);
        redirect("admin_katagori");
    }

    function simpan() {
        $id = $this->input->post('id');

        $data = array(
            'kode_katagori' => $this->input->post('kode_katagori'),
            'nama_katagori' => $this->input->post('nama_katagori'),
            'parentid' => $this->input->post('parent_katagori')
        );

        if ($id == "") {
            $this->mkatagori->insert($data);
        } else {
            $this->mkatagori->update($data, $id);
        }

        redirect("admin_katagori");
    }

}

?>
